//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include "CVariableOfWStringBuf.h"
#include "CObjInfoList.h"

UInt32 CVariableOfWStringBuf::AddRef()
{
    Int32 nRef = m_cRef.Increment();
    return (UInt32)nRef;
}

UInt32 CVariableOfWStringBuf::Release()
{
    Int32 nRef = m_cRef.Decrement();

    if (0 == nRef) {
        delete this;
    }

    return nRef;
}

PInterface CVariableOfWStringBuf::Probe(REIID riid)
{
    if (riid == EIID_IInterface) {
        return (PInterface)(IVariableOfWStringBuf *)this;
    }
    else if (riid == EIID_IVariableOfWStringBuf) {
        return (IVariableOfWStringBuf *)this;
    }
    else if (riid == EIID_IWStringBufSetter) {
        return (IWStringBufSetter *)this;
    }
    else if (riid == EIID_IWStringBufGetter) {
        return (IWStringBufGetter *)this;
    }

    return NULL;
}

ECode CVariableOfWStringBuf::Aggregate(AggregateType type, PInterface pObj)
{
    return E_NOT_IMPLEMENTED;
}

ECode CVariableOfWStringBuf::GetDomain(PInterface *ppObj)
{
    return E_NOT_IMPLEMENTED;
}

CVariableOfWStringBuf::CVariableOfWStringBuf(
    /* [in] */ PWStringBuf pWsb,
    /* [in] */ Boolean bAlloc)
{
    m_pWsb = pWsb;
    m_bAlloc = bAlloc;
}

CVariableOfWStringBuf::~CVariableOfWStringBuf()
{
    if (m_bAlloc && m_pWsb) {
        WStringBuf::Free(m_pWsb);
    }
}

ECode CVariableOfWStringBuf::GetTypeInfo(
    /* [out] */ IDataTypeInfo ** ppTypeInfo)
{
    return g_objInfoList.AcquireIntrinsicInfo(CarDataType_WChar, ppTypeInfo);
}

ECode CVariableOfWStringBuf::GetPayload(
    /* [out] */ PVoid * pPayload)
{
    if (!pPayload) {
        return E_INVALID_ARGUMENT;
    }

    *pPayload = m_pWsb;
    return NOERROR;
}

ECode CVariableOfWStringBuf::Rebox(
    /* [in] */ PVoid localVariablePtr)
{
    PWStringBuf pWsb = (PWStringBuf)localVariablePtr;
    if (!pWsb || !(pWsb->m_flags & CarQuintetFlag_Type_WChar)
        || !pWsb->GetCapacity()) {
        return E_INVALID_ARGUMENT;
    }

    if (m_bAlloc && m_pWsb) {
        WStringBuf::Free(m_pWsb);
        m_bAlloc = FALSE;
    }

    m_pWsb = pWsb;

    return NOERROR;
}

ECode CVariableOfWStringBuf::GetSetter(
    /* [out] */ IWStringBufSetter ** ppSetter)
{
    if (!ppSetter) {
        return E_INVALID_ARGUMENT;
    }

    this->AddRef();
    *ppSetter = (IWStringBufSetter *)this;
    return NOERROR;
}

ECode CVariableOfWStringBuf::GetGetter(
    /* [out] */ IWStringBufGetter ** ppGetter)
{
    if (!ppGetter) {
        return E_INVALID_ARGUMENT;
    }

    this->AddRef();
    *ppGetter = (IWStringBufGetter *)this;
    return NOERROR;
}

ECode CVariableOfWStringBuf::SetElement(
    /* [in] */ Int32 index,
    /* [in] */ WChar character)
{
    if (index >= m_pWsb->GetLength()) {
        return E_INVALID_ARGUMENT;
    }

    (*m_pWsb)[index] = character;
    return NOERROR;
}

ECode CVariableOfWStringBuf::SetData(
    /* [in] */ WString wstr)
{
    if (wstr.IsNull()) {
        return E_INVALID_ARGUMENT;
    }

    m_pWsb->Copy(wstr);
    return NOERROR;
}

ECode CVariableOfWStringBuf::GetCapacity(
    /* [out] */ Int32 * pCapacity)
{
    if (!pCapacity) {
        return E_INVALID_ARGUMENT;
    }

    *pCapacity = m_pWsb->GetCapacity();
    return NOERROR;
}

ECode CVariableOfWStringBuf::GetLength(
    /* [out] */ Int32 * pLength)
{
    if (!pLength) {
        return E_INVALID_ARGUMENT;
    }

    *pLength = m_pWsb->GetLength();
    return NOERROR;
}

ECode CVariableOfWStringBuf::GetElement(
    /* [in] */ Int32 index,
    /* [out] */ WChar * pWch)
{
    if (index >= m_pWsb->GetLength()) {
        return E_INVALID_ARGUMENT;
    }

    *pWch = (*m_pWsb)[index];
    return NOERROR;
}

ECode CVariableOfWStringBuf::GetData(
    /* [out] */ WStringBuf * wsBuf)
{
    if (!wsBuf) {
        return E_INVALID_ARGUMENT;
    }

    wsBuf->Copy(m_pWsb->GetPayload());

    return NOERROR;
}
